//
//  ReactNativeParentViewController.m
//  ModifyMetroDemo
//
//  Created by Marcus on 2020/3/8.
//  Copyright © 2020 Facebook. All rights reserved.
//

#import "RNParentViewController.h"
#import "Masonry.h"
#import <React/RCTBridge.h>
#import "AppDelegate.h"
#import "RNChildViewController.h"
#import "RNTimeUtils.h"

@interface RNParentViewController ()

@end

@implementation RNParentViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  [self.navigationItem setTitle:@" Async Load Demo"];
  
  UILabel *label = [[UILabel alloc] init];
  label.text=@"此页面不加载ReactView，仅加载Common";// 文本内容
  label.font = [UIFont systemFontOfSize:18];// 字体大小
  label.textColor = [UIColor blueColor]; // 文本颜色
  [self.view addSubview:label];
  [label mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(10);
    make.right.equalTo(self.view).offset(10);
    make.height.equalTo(@40);
    make.centerX.equalTo(self.view);
    make.centerY.equalTo(self.view).offset(-100);
  }];
  
  UIButton *asyncButton = [[UIButton alloc] init];
  [asyncButton setTitle:@"Async Load (异步加载)" forState:UIControlStateNormal];
  [asyncButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  asyncButton.backgroundColor = [UIColor purpleColor];
  [asyncButton setTag: 1]; //
  [self.view addSubview:asyncButton];
  [asyncButton mas_makeConstraints:^(MASConstraintMaker *make) {
    // make.left.equalTo(self.view);
    // make.right.equalTo(self.view);
    make.width.equalTo(@200);
    make.height.equalTo(@40);
    make.centerX.equalTo(self.view);
    make.centerY.equalTo(self.view).offset(100);
    // make.center.equalTo(self.view);
  }];
  [asyncButton addTarget:self action:@selector(btnClickListener:) forControlEvents:UIControlEventTouchUpInside];
  
  [self initReactNativeContext];
}

- (void) initReactNativeContext{
  [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(handleBridgeDidLoadJavaScriptNotification:)
                                                name:RCTJavaScriptDidLoadNotification
                                              object:nil];
  
  NSDictionary *launchOptions = [[NSDictionary alloc] init];
  
  AppDelegate *myDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
  myDelegate.bridge = [[RCTBridge alloc] initWithDelegate:self
                                            launchOptions:launchOptions];
}

- (void)handleBridgeDidLoadJavaScriptNotification:(NSNotification *)notification
{
   // todo
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
  NSString *commonBundleFile = @"bundle/common.ios";
  return  [[NSBundle mainBundle] URLForResource:commonBundleFile withExtension:@"bundle"];
}

-(void)btnClickListener:(UIButton *) button{
  if(button.tag == 0){
    
  }else if(button.tag == 1){
    // async Button
    NSLog(@"Async Button Clicked.");
    [RNTimeUtils.instance startTimeRecord];
    RNChildViewController *controller = [[RNChildViewController alloc] init];
    [self.navigationController pushViewController:controller animated:NO];
  }else{
    // nothing to do.
  }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
